﻿using Serilog;
using Serilog.Core;
using System;

namespace OkTool.Log
{
    public class SerilogUtil
    {

        /// <summary>
        /// 创建logger
        /// </summary>
        /// <param name="path">默认AppDomain.CurrentDomain.BaseDirectory+ $"/logs/{DateTime.Now.ToString("yyyy-MM-dd")}/log.txt"</param>
        /// <param name="enableFileSizeLimit"></param>
        /// <param name="retainedFileCountLimit"></param>
        /// <param name="fileSizeLimitBytes">文件大小限制默认10MB</param>
        /// <param name="enableRollingInterval"></param>
        /// <returns></returns>
        public static Logger CreateLogger(string path=null,  bool enableFileSizeLimit = false, int retainedFileCountLimit = 0, long fileSizeLimitBytes = 1024 * 1024 * 10, bool enableRollingInterval = false)
        {
            if (string.IsNullOrEmpty(path))
            {
                path = AppDomain.CurrentDomain.BaseDirectory+ $"/logs/{DateTime.Now.ToString("yyyy-MM-dd")}/log.txt";
            }
            var loggerConfiguration = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.File(path,
                    rollingInterval: enableRollingInterval ? RollingInterval.Day : RollingInterval.Infinite,
                    rollOnFileSizeLimit: enableFileSizeLimit,
                    fileSizeLimitBytes: enableFileSizeLimit ? fileSizeLimitBytes : (long?)null,
                    retainedFileCountLimit: enableFileSizeLimit && retainedFileCountLimit > 0 ? retainedFileCountLimit : (int?)null);

            return loggerConfiguration.CreateLogger();
        }
        public static Logger BaseLogger = CreateLogger();
    }
}
